<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Moderators extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('moderators_mod');
    }

    /**
     *   
     *   
     *    
     */
    public function index() {
        $header['title'] = "Welcome to Administration :: Moderators pages";
        $header['keywords'] = "Administration, ";
        $header['description'] = "Administration pages ";
        $data['header'] = $header;

        $data['menu_page'] = "moderators";

        // Definisi koji se view ucitava
        $data['contents'] = 'moderators/moderators_home';  // OVO PROVERITI

        $this->load->view('main', $data);
    }

    public function activity() {
        $header['title'] = "Welcome to Administration :: Moderators pages";
        $header['keywords'] = "Administration, ";
        $header['description'] = "Administration pages ";
        $data['header'] = $header;

        $data['menu_page'] = "moderators";
        $data['statistic'] = NULL;
        $data['teams']=$this->moderators_mod->getTeams();
        // Definisi koji se view ucitava
        $data['contents'] = 'moderators/activity';  // OVO PROVERITI

        $this->load->view('main', $data);
    }

    public function filter_submit() {
        try {
            $filter = $this->input->post('filter');
            $data['statistic'] = $this->moderators_mod->getStatistic($filter);
            $html = $this->load->view('moderators/statistic_list', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => "",
                'num_users' => 0,
                'html' => $html
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'num_users' => 0,
                'html' => ''
            );
        }
        echo json_encode($response);
    }

    public function get_Conversation() {
        try {
            $fromId = $this->input->post('userFrom');
            $toId = $this->input->post('userTo');
            if ($fromId == NULL || $fromId == "" || $toId == NULL || $toId == "")
                $data['messages'] = NULL;
            else
                $data['messages'] = $this->moderators_mod->getConversation($fromId, $toId);

            $user_data = $this->moderators_mod->getUserData($fromId);
            $data['zip_code'] = $user_data->zip_code;
            $data['coins'] = $user_data->coins;
            $data['description'] = $user_data->description;
            $data['id'] = $user_data->id;
            $html = $this->load->view('moderators/messages_list', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => "",
                'html' => $html
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }

    public function teams() {
        $header['title'] = "Welcome to Administration :: Moderators pages";
        $header['keywords'] = "Administration, ";
        $header['description'] = "Administration pages ";
        $data['header'] = $header;

        $data['menu_page'] = "moderators";
        $data['moderator_teams'] = $this->moderators_mod->getTeams();

        // Definisi koji se view ucitava
        $data['contents'] = 'moderators/teams/teams';

        $this->load->view('main', $data);
    }

    /**
     *  AJAX - get team data for team dialog
     */
    public function get_team_data() {
        try {
            //$this->load->model('users_mod');
            $id = $this->input->post('id');

            if ($id == "")
                throw new Exception(lang('team_not_selected'));

            $data['team'] = $this->moderators_mod->getOneTeam($id, true);

            //$html = $this->load->view('users/user_form',$data,true);
            $html = "";
            $response = array(
                'ind' => 1,
                'msg' => "",
                'html' => $html
            );
            $response = array_merge($data['team'], $response);
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }

    public function save_team() {
        try {

            $team = $this->input->post('team');
            $this->moderators_mod->saveTeam($team);

            $data['moderator_teams'] = $this->moderators_mod->getTeams();
            $html = $this->load->view('moderators/teams/all_team_list', $data, true);
            $response = array(
                'ind' => 1,
                'msg' => "",
                'html' => $html
            );
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'num_users' => 0,
                'html' => ''
            );
        }
        echo json_encode($response);
    }

}

/* End of file moderators.php */
/* Location: ./application/controllers/moderators.php */